package com.service1.service

import com.base.model.service1.Model_Service1
import com.base.model.service2.Model_Service2
import com.base.service.IRPC_Service1_Dubbo
import com.base.service.IRPC_Service2_Dubbo
import com.service1.db.mybatis.db1.service.MyBatisDB1Service
import com.service1.db.mybatis.db2.service.MyBatisDB2Service
import com.yfree.global.YController
import org.apache.dubbo.config.annotation.DubboReference
import org.apache.dubbo.config.annotation.DubboService
import org.springframework.beans.factory.annotation.Value

@DubboService(group = "service1", version = "1")
class DubboImpl_Service1(
    @Value("\${server.port}") var serverPort: String,
    var myBatisDB1Service: MyBatisDB1Service,
    var myBatisDB2Service: MyBatisDB2Service,
) : YController(), IRPC_Service1_Dubbo {
    @DubboReference(group = "service2", version = "1", check = false)
    lateinit var rpc_Service2_Dubbo: IRPC_Service2_Dubbo

    override fun callService2(): String {
        val yModel = yFromRPC(rpc_Service2_Dubbo.index(), Model_Service2::class.java, "rpc_Service2_Dubbo.index调用失败")
        return yResult(
            result = listOf(
                Model_Service1("Dubbo:Server1Port:$serverPort,${yModel.root.result[0].value}")
            )
        )
    }

    override fun do_seata(doException: String): String {
        print("开始更新")
        if (!myBatisDB1Service.update()) return yResult(code = -1, message = "myBatisDB1Service更新失败")
        if (!myBatisDB2Service.update()) return yResult(code = -1, message = "myBatisDB2Service更新失败")
        print("更新完成")
        if (doException == "1") 1 / 0 // 制造异常触发回滚
        return yResult()
    }
}
